package com.yz.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yz.common.annotation.Login;
import com.yz.common.annotation.LoginUser;
import com.yz.common.entity.*;
import com.yz.common.utils.R;
import com.yz.common.entity.DrillingForm;
import com.yz.common.validator.ValidatorUtils;
import com.yz.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.Min;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 *勘探表单相关操作
 */
@RestController
@RequestMapping("/api")
@Api(tags="勘探表单")
public class ApiSurveyController {
    @Autowired
    private RegionalService regionalService;
    @Autowired
    private RuinService ruinService;
    @Autowired
    private DrillingService drillingService;
    @Autowired
    private DrillingDetailService drillingDetailService;
    @Autowired
    private SectionService sectionService;
    @Autowired
    private CheckService checkService;
    @GetMapping("getSurveyForms")
    @ApiOperation("获取勘探表单列表")
    @Login
    public R getSurveyForms(@RequestHeader("token")String token,@RequestParam("order")int order,@RequestParam(value = "keyWord",required = false) String keyWord,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId, @ApiIgnore @LoginUser UserEntity user){
        int total =0;
        Page pages =null;
        String group = "create_at desc,id desc";
        if(order!=0){
            group = "create_at asc,id asc";
        }
        if(keyWord==null||keyWord.isEmpty()){
            total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("project_id",projectId).eq("type",5).where("type_form<10").eq("create_user",user.getUsername()));
            pages = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("type",5).where("type_form<10").eq("project_id",projectId).eq("create_user",user.getUsername()).orderBy(group));
        }else{
            total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("project_id",projectId).eq("type",5).eq("create_user",user.getUsername()).where("type_form<10 and (number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')"));
            pages = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("type",5).eq("project_id",projectId).eq("create_user",user.getUsername()).where("type_form<10 and (number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')").orderBy(group));
        }
       return R.ok().put("data",pages.getRecords()).put("total",total);
    }

    @PostMapping("postSection")
    @ApiOperation("提交或修改-断片观察记录表")
    @Login
    public R postSection(@RequestHeader("token")String token,@RequestBody SectionForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            t.getSectionEntity().setCreatorId(user.getUserId());
            sectionService.postSection(t,user);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }
    @GetMapping("getSectionDetail")
    @ApiOperation("获取-断片观察记录表-详情")
    @Login
    public R getSectionDetail(@RequestHeader("token")String token,@RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        try {
            SectionForm data = sectionService.getSectionDetail(id);
            return R.ok().put("data",data);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }
    @DeleteMapping("deleteSection")
    @ApiOperation("删除-断片观察记录表")
    @Login
    public R deleteSection(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            sectionService.deleteSection(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @DeleteMapping("deleteDrilling")
    @ApiOperation("删除-钻探记录")
    @Login
    public R deleteDrilling(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
           drillingService.deleteDrilling(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postDrilling")
    @ApiOperation("提交或修改-钻探记录")
    @Login
    public R postDrilling(@RequestHeader("token")String token,@RequestBody DrillingForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            drillingService.postDrilling(t);
            return R.ok().put("data",null);
        }catch (Exception e){
           return R.error().put("data",null);
        }
    }

    @GetMapping("getDrillingDetail")
    @ApiOperation("获取-钻探记录-详情")
    @Login
    public R getDrillingDetail(@RequestHeader("token")String token,@RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        try {
          DrillingForm data = drillingService.postDrillingDetail(id);
          return R.ok().put("data",data);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @DeleteMapping("deleteRuin")
    @ApiOperation("删除-遗址调查记录")
    @Login
    public R deleteRuin(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            ruinService.deleteMe(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postRuin")
    @ApiOperation("提交或修改-遗址调查记录")
    @Login
    public R postRuin(@RequestHeader("token")String token,@RequestBody RuinEntityReturn t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            ruinService.postMe(t,user);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getRuinDetail")
    @ApiOperation("获取-遗址调查记录-详情")
    @Login
    public R getRuinDetail(@RequestHeader("token")String token,@RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        RuinEntityReturn t = ruinService.getDetail(id);
        return R.ok().put("data",t);
    }

    @DeleteMapping("deleteRegional")
    @ApiOperation("删除区域系统调查记录表")
    @Login
    public R deleteRegional(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            regionalService.deleteMe(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postRegional")
    @ApiOperation("提交或修改-区域系统调查记录表")
    @Login
    public R postRegional(@RequestHeader("token")String token,@RequestBody RegionalForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            t.getRegionalEntity().setCreatorId(user.getUserId());
            regionalService.postMe(t,user);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getRegioanlDetail")
    @ApiOperation("获取区域系统调查记录表详情")
    @Login
    public R getRegioanlDetail(@RequestHeader("token")String token,@RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        RegionalForm t = regionalService.getRegioanlDetail(id);
        return R.ok().put("data",t);
    }
}
